package com.wannabuy.mobile.controler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.StrictMode;

public class Tarefa extends AsyncTask<String, String, String>  {
	private Context context;
	private ProgressDialog progress;
	private TarefaInterface tarefaInterface;

	public Tarefa(Context context, TarefaInterface tarefaInterface) {
		this.context = context;
		this.tarefaInterface = tarefaInterface;
	}

	@Override
	protected String doInBackground(String... params) {
		HttpClient httpcliente = new DefaultHttpClient();
		ArrayList<NameValuePair> postParameters;
		String teste = "";
		HttpPost httppost = new HttpPost(
				"http://10.0.2.2:8084/WannabuyServer/Usuario");

		postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("login", params[0]));
		postParameters.add(new BasicNameValuePair("senha", params[1]));
		postParameters.add(new BasicNameValuePair("acao", "logar"));
		try {
			httppost.setEntity(new UrlEncodedFormEntity(postParameters));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HttpResponse resposta;

		StrictMode.ThreadPolicy politica = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(politica);

		try {
			resposta = httpcliente.execute(httppost);

			HttpEntity entidade = resposta.getEntity();

			InputStream dados;

			if (entidade != null) {
				dados = entidade.getContent();
				teste = getStringFromInputStream(dados);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		
		return (teste);
	}

	@Override
	protected void onPreExecute() {
		progress = new ProgressDialog(context);
		progress.show();
	}

	@Override
	protected void onPostExecute(String params) {
		progress.setMessage("Logado com Sucesso!");
		progress.dismiss();
	}

	private static String getStringFromInputStream(InputStream is) {

		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();

		String line;
		try {

			br = new BufferedReader(new InputStreamReader(is));
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return sb.toString();

	}

}
